//当前选中的角色编号
var roleId = 0;

$(function () {
    //获取所有角色
    getAllRole();
});

/**
 * 获取所有角色
 */
function getAllRole() {
    $.post("/super/getAllRole",
        function (data) {
            //先清空
            $("#role tbody").empty();
            $.each(data.rows, function (i, j) {
                var str = "<tr><td><span class='table-pointer' onclick='selectRole(" + j.id + ")'>" + j.name + "</span></td></tr>";
                $("#role").append(str);
            });
        }, "json");

}

/**
 * 选中角色
 * @param roleId 选中角色的编号
 */
function selectRole(roleId) {
    //当前选中角色变更
    this.roleId = roleId;
    refresh();
    /*$("#privilege").bootstrapTable("refresh");*/
}

/**
 * 当前角色所拥有的权限
 */
function getRolePrivilege() {
    $.post("/super/getPrivilegeByRoleId",
        {"roleId": this.roleId},
        function (data) {
            //先清空
            $("#rolePrivilege tbody").empty();
            //父权限
            $.each(data.rows, function (i, j) {
                if (j.parent == null) {
                    var str = "<tr>" +
                        "<td>" + j.name + "</td>" +
                        "<td></td>" +
                        "<td><span class='table-pointer' onclick='removePrivilege(" + j.id + ",\"parent\");'>移除</span></td>" +
                        "</tr>";
                    $("#rolePrivilege").append(str);
                }
                //对应的子权限
                $.each(data.rows, function (index, children) {
                    if (children.parent != null && children.parent.id == j.id) {
                        var str = "<tr>" +
                            "<td></td>" +
                            "<td>" + children.name + "</td>" +
                            "<td><span class='table-pointer' onclick='removePrivilege(" + children.id + ",\"children\");'>移除</span></td>" +
                            "</tr>";
                        $("#rolePrivilege").append(str);
                    }
                });
            });
        }, "json");
}

/**
 * 移除这名角色当前所拥有的一个权限
 * @param privilegeId 将要移除的权限编号
 */
function removePrivilege(privilegeId, level) {
    //this.roleId和swal有冲突，所以再次定义一个无冲突的变量
    var removeRoleId = this.roleId;
    swal({
            title: "确认要移除权限吗?",
            text: "如果这是父权限，那么会连对应的子权限子一并移除!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function (isConfirm) {
            if (isConfirm) {
                $.post("/super/removePrivilege",
                    {
                        "roleId": removeRoleId,
                        "privilegeId": privilegeId,
                        "level": level
                    }, function (data) {
                        if (data.updates > 0) {
                            swal("删除成功！", "您已删除了一组权限！", "success");
                            //刷新表格
                            refresh();
                        } else {
                            swal("删除失败！", "删除失败,请重试！", "error");
                            //刷新表格
                            refresh();
                        }
                    }, "json");
            } else {
                swal("移除失败", "您取消了操作!", "error");
            }
        });
}

/**
 * 表格刷新
 */
function refresh() {
    //刷新拥有的权限
    getRolePrivilege();
    //刷新未拥有的权限
    getPartPrivilegeNotRoleId();
}

/**
 * 获取当前角色未拥有的权限
 */
function getPartPrivilegeNotRoleId() {
    $.post("/super/getPartPrivilegeNotRoleId",
        {
            "roleId": this.roleId
        }, function (data) {
            //先清空
            $("#privilege tbody").empty();
            //加载父权限
            $.each(data.rows, function (i, j) {
                if (j.parent == null) {
                    var str = "<tr>" +
                        "<td>" + j.name + "</td>" +
                        "<td></td>" +
                        "<td><span class='table-pointer' onclick='addPrivilege(" + j.id + ",\"parent\");'>加入</span></td>" +
                        "</tr>";
                    $("#privilege").append(str);
                }
                //加载对应的子权限
                $.each(data.rows, function (index, children) {
                    if (children.parent != null && children.parent.id == j.id) {
                        var str = "<tr>" +
                            "<td></td>" +
                            "<td>" + children.name + "</td>" +
                            "<td><span class='table-pointer' onclick='addPrivilege(" + children.id + ",\"children\")'>加入</span></td>" +
                            "</tr>";
                        $("#privilege").append(str);
                    }
                });
            });
        }, "json");
}

/**
 * 为当前角色分配一组权限
 * @param PrivilegeId 权限编号
 */
function addPrivilege(privilegeId, level) {
    $.post("/super/appendPrivilege",
        {
            "privilegeId": privilegeId,
            "roleId": this.roleId,
            "level": level
        }, function (data) {
            if (data.updates > 0) {
                swal("分配成功！", "您已为当前角色分配了一组权限！", "success");
                refresh();
            } else {
                swal("分配失败！", "服务器忙,请稍后再试！", "success");
                refresh();
            }
        }, "json");
}